define(function(require, exports, module) {

	var $ = require('$'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        ContactModel = require('model/contact'),
        ContactCollection = require('collection/contact'),
        ContactView = require('view/contact');

	module.exports = Backbone.View.extend({
        el: '.page[data-page=contacts]',
		events: { },
        template: require('tpl/contact/list.html'),
        contactList: new ContactCollection,
        initialize: function(options) {
            var self = this;
            
            //传入gomac
            this.gomac = options.gomac;

        },
        render: function() {
            var self = this;
            //初始化视图内容
            this.$el.find('.page-content .contacts-block').html( this.template );
            //获取数据
            this.contactList.fetch({
                success: function(collection, response, options) {
                    //获取数据成功
                    if(collection.length > 0) {
                        _.each(collection.models, function(model){ self.addOne(model); });
                        self.$el.find('.page-content .content-block p').text(collection.length+'位联系人');
                    } else {
                        self.$el.find('.page-content .content-block p').text('还没有联系人');
                    }
                },
                error: function(collection, response, options) {
                    //错误发生了
                    self.gomac.alert('获取联系人时出错');
                    console.log('获取联系人时出错', collection, response, options);
                }
            });
        },
        addOne: function(model) {
            var self = this;
            var view = new ContactView({
                model: model,
                gomac: self.gomac
            });
            var selector = '.page-content .list-group.list-'+model.get('pinyin').substr(0,1).toUpperCase();
            self.$el.find(selector).css({display:'block'}).find('ul').append( view.render().el );
        }
	});
});